<template>
  <div>



    <el-row :gutter="15">
      <el-form style="color: black!important;" ref="elForm" :model="dataForm" :rules="dataRule" size="medium"
               label-width="140px" label-position="right"
               :disabled="setting.readonly"
      >
        <template v-if="!loading && formOperates">
          <el-col :span="24">
            <el-tabs v-model="activezizgbo" tab-position="top" class="mb-20">
              <div v-if="active==0">
                <el-col :span="6" v-if="judgeShow('contractNumber')"
                >
                  <el-form-item label="合同编号"
                                prop="contractNumber"
                  >
                    <el-input v-model="dataForm.contractNumber"
                              placeholder="请输入" :disabled="judgeWrite('contractNumber')" clearable
                              :style='{"width":"100%"}'
                    >

                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="judgeShow('contractType')"
                >
                  <el-form-item label="合同类型"
                                prop="contractType"
                  >
                    <el-select v-model="dataForm.contractType"
                               placeholder="请选择" :disabled="judgeWrite('contractType')" clearable
                               :style='{"width":"100%"}'
                    >
                      <el-option v-for="(item, index) in contractTypeOptions" :key="index" :label="item.fullName"
                                 :value="item.id" :disabled="item.disabled"
                      ></el-option>

                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="judgeShow('project')"
                >
                  <el-form-item label="项目"
                                prop="project"
                  >
                    <el-select v-model="dataForm.project"
                               placeholder="请选择" :disabled="judgeWrite('project')" clearable :style='{"width":"100%"}'
                    >
                      <el-option v-for="(item, index) in projectOptions" :key="index" :label="item.fullName"
                                 :value="item.id" :disabled="item.disabled"
                      ></el-option>

                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="judgeShow('relatedContracts')"
                >
                  <el-form-item label="关联合同"
                                prop="relatedContracts"
                  >
                    <el-select v-model="dataForm.relatedContracts"
                               placeholder="请选择" :disabled="judgeWrite('relatedContracts')" clearable
                               :style='{"width":"100%"}'
                    >
                      <el-option v-for="(item, index) in relatedContractsOptions" :key="index" :label="item.fullName"
                                 :value="item.id" :disabled="item.disabled"
                      ></el-option>

                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8" v-if="judgeShow('signingDate')"
                >
                  <el-form-item label="签订日期"
                                prop="signingDate"
                  >
                    <el-date-picker v-model="dataForm.signingDate"
                                    placeholder="请选择" :disabled="judgeWrite('signingDate')" clearable
                                    :style='{"width":"100%"}' type="date" format="yyyy-MM-dd" value-format="timestamp"
                    >

                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="8" v-if="judgeShow('effectiveDate')"
                >
                  <el-form-item label="生效日期"
                                prop="effectiveDate"
                  >
                    <el-date-picker v-model="dataForm.effectiveDate"
                                    placeholder="请选择" :disabled="judgeWrite('effectiveDate')" clearable
                                    :style='{"width":"100%"}' type="date" format="yyyy-MM-dd" value-format="timestamp"
                    >

                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="8" v-if="judgeShow('purposeHouse')"
                >
                  <el-form-item label="房屋用途"
                                prop="purposeHouse"
                  >
                    <el-input v-model="dataForm.purposeHouse"
                              placeholder="请输入" :disabled="judgeWrite('purposeHouse')" clearable
                              :style='{"width":"100%"}'
                    >

                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="14" v-if="judgeShow('contractHandler')"
                >
                  <el-form-item label="合同经办人"
                                prop="contractHandler"
                  >
                    <user-select v-model="dataForm.contractHandler"
                                 placeholder="请选择" :disabled="judgeWrite('contractHandler')" clearable
                    >

                    </user-select>
                  </el-form-item>
                </el-col>
                <el-col :span="10" v-if="judgeShow('ihis')"
                >
                  <el-form-item label="是否为历史合同"
                                prop="ihis"
                  >
                    <el-switch v-model="dataForm.ihis"
                               :disabled="judgeWrite('ihis')" :active-value="1" :inactive-value="0"
                    >

                    </el-switch>
                  </el-form-item>
                </el-col>
                <el-col :span="14" v-if="judgeShow('notes')"
                >
                  <el-form-item label="备注"
                                prop="notes"
                  >
                    <el-input v-model="dataForm.notes"
                              placeholder="请输入" :disabled="judgeWrite('notes')" :style='{"width":"100%"}' true
                              type="textarea" :autosize='{"minRows":4,"maxRows":4}'
                    >

                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-card class="mb-20" shadow="always" header="甲方">
                    <el-col :span="10" v-if="judgeShow('partyName')"
                    >
                      <el-form-item label="甲方名称"
                                    prop="partyName"
                      >
                        <el-select v-model="dataForm.partyName"
                                   placeholder="请选择" :disabled="judgeWrite('partyName')" clearable
                                   :style='{"width":"100%"}'
                        >
                          <el-option v-for="(item, index) in partyNameOptions" :key="index" :label="item.fullName"
                                     :value="item.id" :disabled="item.disabled"
                          ></el-option>

                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="10" v-if="judgeShow('partyBusinessLicense')"
                    >
                      <el-form-item label="甲方营业执照"
                                    prop="partyBusinessLicense"
                      >
                        <el-input v-model="dataForm.partyBusinessLicense"
                                  placeholder="请输入" :disabled="judgeWrite('partyBusinessLicense')" clearable
                                  :style='{"width":"100%"}'
                        >

                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="10" v-if="judgeShow('partyContacts')"
                    >
                      <el-form-item label="甲方联系人"
                                    prop="partyContacts"
                      >
                        <el-input v-model="dataForm.partyContacts"
                                  placeholder="请输入" :disabled="judgeWrite('partyContacts')" clearable
                                  :style='{"width":"100%"}'
                        >

                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="10" v-if="judgeShow('partyContactInformation')"
                    >
                      <el-form-item label="甲方联系方式"
                                    prop="partyContactInformation"
                      >
                        <el-input v-model="dataForm.partyContactInformation"
                                  placeholder="请输入" :disabled="judgeWrite('partyContactInformation')" clearable
                                  :style='{"width":"100%"}'
                        >

                        </el-input>
                      </el-form-item>
                    </el-col>
                  </el-card>
                </el-col>
                <el-col :span="24">
                  <el-card class="mb-20" shadow="always" header="乙方客户信息">
                    <el-col :span="24" v-if="judgeShow('zcglhtglpabList') && formOperates">
                      <el-form-item label-width="0">
                        <div class="JNPF-common-title">
                          <h2></h2>
                        </div>
                        <el-table :data="dataForm.zcgl_htgl_pab" size="mini">
                          <el-table-column type="index" width="70" label="序号" align="center"/>
                          <el-table-column prop="pabCustomerName" label="乙方客户名称" width="180" align="center"
                                           v-if="judgeShow('zcglhtglpabList-pabCustomerName')">
                            <template slot-scope="scope">
                              <el-form-item :prop="'zcgl_htgl_pab.' + scope.$index + '.pabCustomerName'"
                              >
                                <el-select v-model="scope.row.pabCustomerName" placeholder="请选择"
                                           :disabled="judgeWrite('zcglhtglpabList') || judgeWrite('zcglhtglpabList-pabCustomerName')"
                                           clearable :style='{"width":"100%"}'
                                           @change="handleCustomerNameChange(scope.$index, scope.row)">
                                  <el-option v-for="(item, index) in kfList" :key="index" :label="item.fullName" :value="item.id" :disabled="item.disabled">
                                  </el-option>
                                </el-select>
                              </el-form-item>
                            </template>
                          </el-table-column>
                          <el-table-column prop="pabContactNumber" label="乙方联系电话"
                                           v-if="judgeShow('zcglhtglpabList-pabContactNumber')"
                          >

                            <template slot-scope="scope">
                              <el-form-item :prop="'zcgl_htgl_pab.' + scope.$index + '.pabContactNumber'"
                              >
                                <el-input v-model="scope.row.pabContactNumber"
                                          placeholder="请输入"
                                          :disabled="judgeWrite('zcglhtglpabList') || judgeWrite('zcglhtglpabList-pabContactNumber')"
                                          clearable :style='{"width":"100%"}'
                                >
                                </el-input>
                              </el-form-item>
                            </template>
                          </el-table-column>

                          <el-table-column prop="pabIdType" label="乙方证件类型" v-if="judgeShow('zcglhtglpabList-pabIdType')"
                          >
                            <template slot="header" v-if="judgeRequired('zcglhtglpabList-pabIdType')">
                              <span class="required-sign">*</span>乙方证件类型
                            </template>
                            <template slot-scope="scope">
                              <el-select v-model="scope.row.pabIdType"
                                         placeholder="请选择"
                                         :disabled="judgeWrite('zcglhtglpabList') || judgeWrite('zcglhtglpabList-pabIdType')"
                                         clearable :style='{"width":"100%"}'
                              >
                                <el-option v-for="(item, index) in pabIdTypeOptions" :key="index" :label="item.fullName"
                                           :value="item.id" :disabled="item.disabled"
                                ></el-option>
                              </el-select>
                            </template>
                          </el-table-column>
                          <el-table-column prop="pabIdNo" label="乙方证件号码" v-if="judgeShow('zcglhtglpabList-pabIdNo')"
                          >
                            <template slot="header" v-if="judgeRequired('zcglhtglpabList-pabIdNo')">
                              <span class="required-sign">*</span>乙方证件号码
                            </template>
                            <template slot-scope="scope">
                              <el-input v-model="scope.row.pabIdNo"
                                        placeholder="请输入"
                                        :disabled="judgeWrite('zcglhtglpabList') || judgeWrite('zcglhtglpabList-pabIdNo')"
                                        clearable :style='{"width":"100%"}'
                              >
                              </el-input>
                            </template>
                          </el-table-column>
                          <el-table-column prop="pabContactAddress" label="乙方联系地址"
                                           v-if="judgeShow('zcglhtglpabList-pabContactAddress')"
                          >
                            <template slot="header" v-if="judgeRequired('zcglhtglpabList-pabContactAddress')">
                              <span class="required-sign">*</span>乙方联系地址
                            </template>
                            <template slot-scope="scope">
                              <el-input v-model="scope.row.pabContactAddress"
                                        placeholder="请输入"
                                        :disabled="judgeWrite('zcglhtglpabList') || judgeWrite('zcglhtglpabList-pabContactAddress')"
                                        clearable :style='{"width":"100%"}'
                              >
                              </el-input>
                            </template>
                          </el-table-column>
                          <el-table-column label="操作" width="70"
                                           v-if="!setting.readonly && !judgeWrite('zcglhtglpabList')"
                          >
                            <template slot-scope="scope">
                              <el-button size="mini" type="text" class="JNPF-table-delBtn"
                                         @click="delzcglhtglpabList(scope.$index)"
                              >删除
                              </el-button>
                            </template>
                          </el-table-column>
                        </el-table>
                        <div class="table-actions" @click="addzcglhtglpabList()">
                          <el-button type="text" icon="el-icon-plus">添加</el-button>
                        </div>
                      </el-form-item>
                    </el-col>
                  </el-card>
                </el-col>
              </div>

            </el-tabs>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <UserBox v-if="userBoxVisible" ref="userBox" @submit="submit"/>


  </div>
</template>
<script>
import request from '@/utils/request'
import { getDataInterfaceRes } from '@/api/systemData/dataInterface'
import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import Popup from './PopupSelect'
import PrintBrowse from '@/components/PrintBrowse'
import ElTreeSelect from '@/components/JNPF-treeSelect'

export default {
  components: { ElTreeSelect, Popup },
  props: [],
  data() {
    return {
      active: 0,
      dzItem: {},
      dzForm: {},
      dzDialog: false,
      spanArr: [], // 用于合并单元格的数组
      position: 0, // 用于记录位置
      skjh: [],
      lastmodifytimecolumnOptions: [
        {
          value: 'fid',
          label: 'ID'
        },
        {
          value: 'zcname',
          label: '资产名称'
        }
        , {
          value: 'zclabel',
          label: '资产标签'
        }
        ,
        {
          value: 'zcnumber',
          label: '资产编码'
        },

        {
          value: 'address',
          label: '地址'
        },
        {
          value: 'landarea',
          label: '面积'
        }
      ],

      kfList: [],
      visible: false,
      loading: false,
      setting: {},
      eventType: '',
      userBoxVisible: false,
      currTableConf: {},
      addTableConf: {
        zcglhtglpabspaceList: {
          'popupType': 'dialog',
          'hasPage': true,
          'popupTitle': '选择数据',
          'pageSize': 20,
          'columnOptions': [],
          'interfaceId': '',
          'interfaceName': '',
          'relationOptions': [],
          'templateJson': [],
          'popupWidth': '800px'
        }

      },
      tableRows: {
        zcglhtglpabList: {
          pabContactNumber: '',
          pabIdNo: '',
          pabContactAddress: '',
          enabledmark: undefined
        },
        zcglhtglpabspaceList: {
          spaceName: '',
          operatingArea: '',
          spaceType: '',
          rentalStatus: '',
          enabledmark: undefined
        }
      },
      dataForm: {
        id: '',
        flowId: '',
        status: 1,
        freeapproveruserid: '',
        flowUrgent: 1,
        contractNumber: undefined,
        contractType: undefined,
        project: undefined,
        relatedContracts: undefined,
        signingDate: undefined,
        effectiveDate: undefined,
        purposeHouse: undefined,
        contractHandler: undefined,
        ihis: 0,
        notes: undefined,
        partyName: undefined,
        partyBusinessLicense: undefined,
        partyContacts: undefined,
        partyContactInformation: undefined,
        startTime: undefined,
        endTime: undefined,
        paymentDeadlineType: undefined,
        paymentDeadline: undefined,
        paymentDeadlineStatus: undefined,
        paymentZdrq: undefined,
        zcgl_htgl_pab: [],
        zcglhtglpabspaceList: [],
        zcgl_htgl_pab_fytk:
          [{
            startTime: '',
            endTime: '',
            taxRate: '',
            hsUnit: '',
            bhsUnit: '',
            bhsUnitPrice: '',
            hsUnitPrice: ''
          }],
        jnpf_zcgl_htgl_pab_fytk_jnpf_startTime: '',
        jnpf_zcgl_htgl_pab_fytk_jnpf_endTime: '',
        jnpf_zcgl_htgl_pab_fytk_jnpf_taxRate: '',
        jnpf_zcgl_htgl_pab_fytk_jnpf_hsUnit: '',
        jnpf_zcgl_htgl_pab_fytk_jnpf_bhsUnit: ''
      },
      tableRequiredData: {},
      activezizgbo: '0',
      dataRule:
        {
          'zcgl_htgl_pab_fytk.*.relatedSpace': [
            { type: 'array', required: true, message: '请选择关联资产', trigger: 'change' }
          ],

          contractType: [
            {
              required: true,
              message: '请选择',
              trigger: 'change'
            }
          ],
          project: [
            {
              required: true,
              message: '请选择',
              trigger: 'change'
            }
          ],
          partyName: [
            {
              required: true,
              message: '请选择',
              trigger: 'change'
            }
          ],
          partyContactInformation: [
            {
              required: true,
              message: '请输入',
              trigger: 'blur'
            },
            {
              pattern: /^1[3456789]\d{9}$/,
              message: '请输入正确的手机号码',
              trigger: 'blur'
            }
          ],
          startTime: [
            {
              required: true,
              message: '请选择开始日期',
              trigger: 'change'
            }
          ],
          endTime: [
            {
              required: true,
              message: '请选择',
              trigger: 'change'
            }
          ]

        },
      contractTypeOptions: [],
      projectOptions: [],
      relatedContractsOptions: [{ 'fullName': '选项一', 'id': '1' }, { 'fullName': '选项二', 'id': '2' }],
      partyNameOptions: [{ 'fullName': '选项一', 'id': '1' }, { 'fullName': '选项二', 'id': '2' }],
      paymentDeadlineTypeOptions: [],
      paymentDeadlineStatusOptions: [],

      pabCustomerNameOptions: [{ 'fullName': '选项一', 'id': '1' }, { 'fullName': '选项二', 'id': '2' }],
      pabIdTypeOptions: [{ 'fullName': '选项一', 'id': '1' }, { 'fullName': '选项二', 'id': '2' }]
    }
  },
  computed: {
    formOperates() {
      return this.setting.formOperates
    }
  },
  watch: {},
  created() {
    this.getcontractTypeOptions()
    this.getprojectOptions()
    this.getpaymentDeadlineTypeOptions()
    this.getpaymentDeadlineStatusOptions()

    this.getKhList()

  },
  mounted() {
  },
  methods: {

    getItemProp(index, prop) {
      return `zcgl_htgl_pab.${index}.${prop}`;
    },
    nextS() {

      if (this.active-- < 1) this.active = 0
    },

    next() {
      if (this.active++ > 2) this.active = 3
    },
    submitDz() {
      const contractStartTime = new Date(this.dzItem.startTime) // 合同开始时间
      const contractEndTime = new Date(this.dzItem.endTime) // 合同结束时间
      const paymentCycle = parseInt(this.dzItem.paymentCycle) // 付款周期，单位：月
      const paymentType = this.dzItem.paymentType // '账单开始前' 或 '账单开始后'
      const monthlyRent = parseFloat(this.dzItem.monthlyRent) // 月租金
      const paymentDays = parseInt(this.dzItem.paymentDays) // 付款天数
      const dzStartTime = new Date(this.dzForm.startTime) // 递增开始时间
      const dzPercent = parseFloat(this.dzForm.dz) / 100 // 递增百分比，转换为小数

      // 计算所有可能的递增开始日期
      let possibleDzStartTimes = []
      possibleDzStartTimes.push(this.formatDate(contractStartTime))
      let tempDate = new Date(contractStartTime)
      while (tempDate < contractEndTime) {
        tempDate.setMonth(tempDate.getMonth() + paymentCycle)
        if (tempDate <= contractEndTime) {
          possibleDzStartTimes.push(this.formatDate(new Date(tempDate)))
        }
      }

      // 检查用户输入的递增开始日期是否在可能的日期列表中
      let valid = false
      for (let date of possibleDzStartTimes) {
        if (this.formatDate(new Date(dzStartTime)) == date) {
          valid = true
          break
        }
      }

      if (!valid) {
        this.$message({
          message: '递增开始日期必须是合同开始日期加上付款周期后的第一天',
          type: 'error',
          duration: 1000
        })
        return
      }

      const intervals = []
      let currentStart = new Date(contractStartTime)
      let currentRent = monthlyRent // 当前租金

      while (currentStart < contractEndTime) {
        let currentEnd = new Date(currentStart)
        currentEnd.setMonth(currentStart.getMonth() + paymentCycle)

        // 确保结束时间不超过合同结束时间
        if (currentEnd > contractEndTime) {
          currentEnd = new Date(contractEndTime)
        }

        const interval = {
          start: this.formatDate(currentStart), // 使用自定义格式化方法
          end: this.formatDate(currentEnd), // 使用自定义格式化方法
          paymentDate: null,
          rent: currentRent.toFixed(2),
          increasePercent: 0
        }

        // 计算付款日期
        if (paymentType === '账单开始前') {
          interval.paymentDate = new Date(currentStart)
          interval.paymentDate.setDate(interval.paymentDate.getDate() - paymentDays)
        } else if (paymentType === '账单开始后') {
          interval.paymentDate = new Date(currentStart)
          interval.paymentDate.setDate(interval.paymentDate.getDate() + paymentDays)
        }

        // 将付款日期转换为字符串格式
        interval.paymentDate = this.formatDate(interval.paymentDate)

        // 检查是否需要递增
        if (currentStart >= dzStartTime) {
          currentRent = currentRent * (1 + dzPercent) // 更新当前租金
          interval.increasePercent = dzPercent * 100
        }

        intervals.push(interval)

        // 更新当前开始时间为当前结束时间的下一天
        currentStart = new Date(currentEnd)
        // currentStart.setDate(currentStart.getDate() + 1);
        currentStart.setDate(currentStart.getDate())
      }

      console.log(intervals)

      for (let i = 0; i < this.dataForm.zcgl_htgl_pab_fytk.length; i++) {

        if (this.dzItem.relatedSpace == this.dataForm.zcgl_htgl_pab_fytk[i].relatedSpace) {

          this.dataForm.zcgl_htgl_pab_fytk[i].listDz = intervals

        }

      }
      this.dzDialog = false
      this.$forceUpdate()
      // 这里可以将 intervals 赋值给某个数据属性，以便在前端展示
    },

    formatDate(date) {
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      return `${year}-${month}-${day}`
    },

    onClose() {
      this.dzDialog = false

    },

    addDz(item) {

      this.dzDialog = true
      this.dzItem = item
    },

    toJsDj(item) {

      if (item.relatedSpace == '' || item.relatedSpace == null || item.relatedSpace == undefined) {

        this.$message({
          message: '请先选择关联资产',
          type: 'error',
          duration: 1000
        })
        item.hsUnitPrice = ''
        item.monthlyRent = ''
        return
      }

      if (item.bhsUnit == '0') {

        item.monthlyRent = (item.hsUnitPrice * item.landarea).toFixed(2)

      } else {

        item.monthlyRent = item.hsUnitPrice.toFixed(2)

      }

    },

    toGetArea(item) {
      var landarea = this.dataForm.zcglhtglpabspaceList.filter(item => item.id === item.relatedSpace).landarea
      if (landarea == null) {
        item.landarea = 100

      }
    },

    // 计算需要合并的单元格
    getSpanArr(data) {
      data.forEach((item, index) => {
        if (index === 0) {
          this.spanArr.push(1)
          this.position = 0
        } else {
          // 与上一行资产ID相同，合并
          if (data[index].relatedSpace === data[index - 1].relatedSpace) {
            this.spanArr[this.position] += 1
            this.spanArr.push(0)
          } else {
            this.spanArr.push(1)
            this.position = index
          }
        }
      })
    },
    // 定义合并规则
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const _row = this.spanArr[rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    },

    addFytk() {
      this.dataForm.zcgl_htgl_pab_fytk.push({
        relatedSpace: [],
        startTime: null
      })
    },
    removeFytk(index) {
      this.dataForm.zcgl_htgl_pab_fytk.splice(index, 1)
    },

    handleAddList(checkedRow) {
      // 接收子组件传递的 checkedRow 数据
      this.dataForm.zcgl_htgl_pab_space1 = checkedRow
      // 你可以在这里处理接收到的数据
    },
    handleCustomerNameChange(index, row) {
      if (!row.pabCustomerName) {
        // 如果选择的客户名称为空，可以清空其他字段
        row.otherField = ''
        return
      }
      // 调用接口获取其他字段的值
      this.fetchOtherField(row.pabCustomerName).then(response => {

        // 假设接口返回的数据包含 otherField 的值
        row.pabContactNumber = response.data.zcgl_customer_user.contactNumber
      }).catch(error => {
        console.error('Failed to fetch other field:', error)
      })
    },
    getKhList() {

      request({
        url: '/api/example/ZcglCustomer/getAllList',
        method: 'post'
      }).then(res => {
        this.kfList = res.data
      })

    },
    fetchOtherField(customerId) {

      return request({
        url: '/api/example/ZcglCustomer/detail/' + customerId,
        method: 'get'
      })

      // 根据 customerId 调用接口

    },

    judgeShow(id) {
      if (!this.setting.formOperates || !this.setting.formOperates.length) return true
      let arr = this.setting.formOperates.filter(o => o.id === id) || []
      if (!arr.length) return true
      let item = arr[0]
      return item.read
    },
    judgeWrite(id) {
      if (!this.setting.formOperates || !this.setting.formOperates.length) return false
      let arr = this.setting.formOperates.filter(o => o.id === id) || []
      if (!arr.length) return true
      let item = arr[0]
      return !item.write
    },
    getcontractTypeOptions() {
      getDictionaryDataSelector('622532919440706373').then(res => {
        this.contractTypeOptions = res.data.list
      })
    },
    getprojectOptions() {
      getDictionaryDataSelector('622532443626277701').then(res => {
        this.projectOptions = res.data.list
      })
    },
    getpaymentDeadlineTypeOptions() {
      getDictionaryDataSelector('622533108956138312').then(res => {
        this.paymentDeadlineTypeOptions = res.data.list
      })
    },
    getpaymentDeadlineStatusOptions() {
      getDictionaryDataSelector('622533108956138313').then(res => {
        this.paymentDeadlineStatusOptions = res.data.list
      })
    },
    top() {
      if (parseInt(this.activezizgbo) > 0) {
        this.activezizgbo = (parseInt(this.activezizgbo) - 1).toString()
      }
      // this.$emit('refresh')

    },
    createSk() {
      var data = JSON.parse(JSON.stringify(this.dataForm))


      data.zcIds = this.dataForm.zcgl_htgl_pab_space1
        .map(item => item.id) // 提取每个元素的 `id` 字段
        .join(',') // 用逗号拼接成字符串

      for (let i = 0; i < this.dataForm.zcgl_htgl_pab_fytk.length; i++) {
        data.zcgl_htgl_pab_fytk[i].relatedSpace = this.dataForm.zcgl_htgl_pab_fytk[i].relatedSpace.join(',')

      }

      request({
        url: '/api/example/ZcglHtglInfo/createSkjh',
        data: data,
        method: 'post'
      }).then(res => {
        this.skjh = res.data.skjhEntitys

        for (let i = 0; i < this.skjh.length; i++) {
          this.getSpanArr(this.skjh[i].listSk)
        }

        this.loading = false
        this.active = 2
      })

      console.log(JSON.stringify(this.dataForm) + '操你妈')
    },
    // next() {
    //   // this.$emit('refresh')
    //   if (parseInt(this.activezizgbo) < 4) {
    //     this.activezizgbo = (parseInt(this.activezizgbo) + 1).toString()
    //   }
    // },
    init(data) {
      this.dataForm.id = data.id || ''
      this.setting = data
      this.visible = true
      this.updateDataRule()
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          request({
            url: '/api/example/ZcglHtglInfo/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataInfo(res.data)
            this.loading = false
          })
        }
        this.$emit('setPageLoad')
      })
      this.$store.commit('generator/UPDATE_RELATION_DATA', {})
    },
    updateDataRule() {
      let newRules = {}
      for (let i = 0; i < this.setting.formOperates.length; i++) {
        const item = this.setting.formOperates[i]
        if (!item.id.includes('-')) {
          const newRulesItem = {
            required: item.required,
            message: item.name + '不能为空',
            trigger: item.trigger || 'blur'
          }
          if (!this.dataRule.hasOwnProperty(item.id)) {
            if (item.required) this.$set(newRules, item.id, [newRulesItem])
          } else {
            let withoutRequiredItem = true
            for (let i = 0; i < this.dataRule[item.id].length; i++) {
              if (this.dataRule[item.id][i].hasOwnProperty('required')) {
                this.dataRule[item.id][i].required = item.required
                withoutRequiredItem = false
              }
            }
            if (withoutRequiredItem && item.required) this.dataRule[item.id].push(newRulesItem)
          }
        } else {
          let key = item.id.split('-')[0]
          let newId = item.id.split('-')[1]
          let newItem = {
            ...item,
            id: newId
          }
          if (!this.tableRequiredData.hasOwnProperty(key)) {
            this.$set(this.tableRequiredData, key, [newItem])
          } else {
            this.tableRequiredData[key].push(newItem)
          }
        }
      }
      this.dataRule = { ...this.dataRule, ...newRules }
    },
    exist() {
      let isOk = true
      for (let key in this.tableRequiredData) {
        if (this.dataForm[key] && Array.isArray(this.dataForm[key])) {
          for (let i = 0; i < this.dataForm[key].length; i++) {
            let item = this.dataForm[key][i]
            inner: for (let id in item) {
              let arr = this.tableRequiredData[key].filter(o => o.id === id) || []
              if (!arr.length) continue inner
              if (arr[0].required) {
                let msg = `${arr[0].name}不能为空`
                let boo = true
                if (arr[0].dataType === 'array') {
                  boo = !this.jnpf.isEmptyArray(item[id])
                } else {
                  boo = !this.jnpf.isEmpty(item[id])
                }
                if (!boo) {
                  this.$message({
                    message: msg,
                    type: 'error',
                    duration: 1000
                  })
                  isOk = false
                  break
                }
              }
            }
          }
        }
      }
      return isOk
    },
    judgeRequired(id) {
      if (!this.formOperates || !this.formOperates.length) return false
      let arr = this.formOperates.filter(o => o.id === id) || []
      if (!arr.length) return false
      let item = arr[0]
      return item.required
    },
    // 表单提交
    dataFormSubmit(eventType, flowUrgent) {
      this.$refs['elForm'].validate((valid) => {
        if (valid) {
          if (this.exist && !this.exist()) return
          if (eventType === 'audit' || eventType === 'reject') {
            this.$emit('eventReceiver', this.dataForm, eventType)
            return
          }





          this.dataForm.status = eventType === 'submit' ? 0 : 1
          this.dataForm.flowId = this.setting.flowId
          this.dataForm.flowUrgent = flowUrgent




          this.eventType = eventType
          if (this.eventType === 'submit') {
            this.$confirm('您确定要提交当前流程吗, 是否继续?', '提示', {
              type: 'warning'
            }).then(() => {
              this.request()
            }).catch(() => {
            })
          } else {
            this.request()
          }
        }
      })
    },
    request() {
      var _data = this.dataList()
      alert(JSON.stringify(_data)+"发送")
      var data = _data
      data.zcIds = this.dataForm.zcgl_htgl_pab_space1
        .map(item => item.id) // 提取每个元素的 `id` 字段
        .join(',') // 用逗号拼接成字符串

      for (let i = 0; i < this.dataForm.zcgl_htgl_pab_fytk.length; i++) {
        data.zcgl_htgl_pab_fytk[i].relatedSpace = this.dataForm.zcgl_htgl_pab_fytk[i].relatedSpace.join(',')

      }



      if (!this.dataForm.id) {
        request({
          url: '/api/example/ZcglHtglInfo',
          method: 'post',
          data: data
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.visible = false
              this.$emit('close', true)
            }
          })
        })
      } else {
        request({
          url: '/api/example/ZcglHtglInfo/' + this.dataForm.id,
          method: 'PUT',
          data: _data
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.visible = false
              this.$emit('close', true)
            }
          })
        })
      }
    },
    addzcglhtglpabList() {
      let item = {
        pabCustomerName: undefined,
        pabContactNumber: undefined,
        pabIdType: undefined,
        pabIdNo: undefined,
        pabContactAddress: undefined
      }
      this.dataForm.zcgl_htgl_pab.push(item)
    },
    delzcglhtglpabList(index) {
      this.dataForm.zcgl_htgl_pab.splice(index, 1)
    },
    addzcglhtglpabspaceList() {
      let item = {
        spaceName: undefined,
        operatingArea: undefined,
        spaceType: undefined,
        rentalStatus: undefined
      }
      // this.dataForm.zcglhtglpabspaceList.push(item)
    },
    delzcglhtglpabspaceList(index) {
      this.dataForm.zcgl_htgl_pab_space1.splice(index, 1)
    },
    openSelectDialog(key) {
      this.currTableConf = this.addTableConf[key]
      this.currVmodel = key
      this.selectDialogVisible = true
      this.$nextTick(() => {
        this.$refs.selectDialog.init()
      })
    },
    addForSelect(data) {
      for (let i = 0; i < data.length; i++) {
        let item = { ...this.tableRows[this.currVmodel], ...data[i] }
        this.dataForm[this.currVmodel].push(item)
      }
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm))

      // _data.ihis = parseInt(_data.ihis)
      //
      // _data.zcgl_htgl_pab_fytk.startTime = _data.jnpf_zcgl_htgl_pab_fytk_jnpf_startTime
      // _data.zcgl_htgl_pab_fytk.endTime = _data.jnpf_zcgl_htgl_pab_fytk_jnpf_endTime
      // _data.zcgl_htgl_pab_fytk.taxRate = _data.jnpf_zcgl_htgl_pab_fytk_jnpf_taxRate
      // _data.zcgl_htgl_pab_fytk.hsUnit = _data.jnpf_zcgl_htgl_pab_fytk_jnpf_hsUnit
      // _data.zcgl_htgl_pab_fytk.bhsUnit = _data.jnpf_zcgl_htgl_pab_fytk_jnpf_bhsUnit
      // for (let i = 0; i < _data.zcglhtglpabList.length; i++) {
      //   var _list = _data.zcglhtglpabList[i]
      // }
      // for (let i = 0; i < _data.zcgl_htgl_pab_space1.length; i++) {
      //   var _list = _data.zcgl_htgl_pab_space1[i]
      // }
      return _data
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll
      _dataAll.ihis = parseInt(_dataAll.ihis)
      // _dataAll.jnpf_zcgl_htgl_pab_fytk_jnpf_startTime = _dataAll.zcgl_htgl_pab_fytk.startTime
      // _dataAll.jnpf_zcgl_htgl_pab_fytk_jnpf_endTime = _dataAll.zcgl_htgl_pab_fytk.endTime
      // _dataAll.jnpf_zcgl_htgl_pab_fytk_jnpf_taxRate = _dataAll.zcgl_htgl_pab_fytk.taxRate
      // _dataAll.jnpf_zcgl_htgl_pab_fytk_jnpf_hsUnit = _dataAll.zcgl_htgl_pab_fytk.hsUnit
      // _dataAll.jnpf_zcgl_htgl_pab_fytk_jnpf_bhsUnit = _dataAll.zcgl_htgl_pab_fytk.bhsUnit
      // for (let i = 0; i < _dataAll.zcglhtglpabList.length; i++) {
      //   var _list = _dataAll.zcglhtglpabList[i]
      // }
      // for (let i = 0; i < _dataAll.zcgl_htgl_pab_space1.length; i++) {
      //   var _list = _dataAll.zcgl_htgl_pab_space1[i]
      // }
      this.dataForm = _dataAll
    }
  }
}

</script>
<style scoped>
/* 增加表格行高 */
/deep/ .el-table .el-table__row {
  height: 53px; /* 你想要的行高 */
  border: 1px slategrey solid;
  color: black;
}

/* 如果你还想要调整单元格内部的垂直居中 */
/deep/ .el-table .cell {
  font-size: 16px;
  color: black;
  text-align: center;
  height: 53px; /* 和行高一致 */
  line-height: 44px; /* 垂直居中文本 */

}

/deep/ .el-form-item__label {

  color: black;
}

.fixed-bottom {
  z-index: 100;
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background: white;
  display: flex;
  justify-content: flex-end; /* 按钮靠右对齐 */
  padding: 10px;
  border-top: 1px solid #ccc; /* 可选：添加分割线 */
}

.confirm-button {
  margin-left: 20px;
  padding: 10px 20px;
  background-color: #007BFF;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.confirm-button:hover {
  background-color: #0056b3;
}

.required {
  color: red;
}
</style>
